package com.heliang.ui.info;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;

import net.tsz.afinal.FinalBitmap;
import android.app.Activity;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextPaint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.appxiehui2.R;
import com.heliang.dataaccess.bean.AdvBean;
import com.heliang.dataaccess.bean.AdvFooterBean;
import com.heliang.dataaccess.bean.InfoBean;
import com.heliang.dataaccess.bean.MailListBean;
import com.heliang.util.AppUtil;
import com.heliang.util.Constants;

public class IndexAdapter extends BaseAdapter {

    private List<AdvBean> advTopBeans;
    private List<AdvFooterBean> advFooterBeans;
    private List<InfoBean> infoItemBeans;
    private List<InfoBean> infoBeans;
    private List<MailListBean> mailListBeans;
    private Activity activity;
    private int currentItem = 0;

    private TextView advTitle;

    private List<View> dots;
    private ScheduledExecutorService scheduledExecutorService;

    private ViewPager viewPager;

    private View topAdvView;

    public IndexAdapter(Activity activity, List<AdvFooterBean> advFooterBeans, List<InfoBean> infoItemBeans,
        List<MailListBean> mailListBeans, List<InfoBean> infoBeans) {
        this.advFooterBeans = advFooterBeans;
        this.infoItemBeans = infoItemBeans;
        this.mailListBeans = mailListBeans;
        this.activity = activity;
        this.infoBeans = infoBeans;

    }

    @Override
    public int getCount() {
        int count = 4;
        return count;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        LayoutInflater inflater = activity.getLayoutInflater();
        if (position == 3) {
            rowView = null;
            rowView = inflater.inflate(R.layout.recomand_store, null);
            if (mailListBeans != null && mailListBeans.size() > 0) {
                Gallery aliveMember = (Gallery) rowView.findViewById(R.id.alive_member_people);
                AliveMemberAdapter aliveAdapter = new AliveMemberAdapter(activity, mailListBeans);
                aliveMember.setAdapter(aliveAdapter);
            } else {
                LinearLayout linerLayout = (LinearLayout) rowView.findViewById(R.id.alive_member_layout);
                linerLayout.setVisibility(View.GONE);
            }
        } else {
            rowView = null;
            rowView = inflater.inflate(R.layout.info_list_item, null);
            if (infoBeans != null && infoBeans.size() > 0) {
                rowView.setBackgroundResource(R.drawable.list_item_seletor);
                FrameLayout ll = (FrameLayout) rowView.findViewById(R.id.list_item_head_layout);

                TextView titleTv = (TextView) rowView.findViewById(R.id.recommend_list_item_title_textview);
                TextPaint tp = titleTv.getPaint();
                tp.setFakeBoldText(true);

                TextView descTv = (TextView) rowView.findViewById(R.id.recommend_list_item_desc_textview);
                ImageView pushIconIv = (ImageView) rowView.findViewById(R.id.push_icon);
                ImageView headIv = (ImageView) rowView.findViewById(R.id.list_item_head_image);
                headIv.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.info_default_icon));
                // ImageView clipsIv =
                // (ImageView)rowView.findViewById(R.id.clips_icon);

                descTv.setLines(2);

                InfoBean infoBean = infoBeans.get(position);

                pushIconIv.setVisibility(View.VISIBLE);
                if (!infoBean.isRecommend()) {
                    pushIconIv.setVisibility(View.GONE);
                }
                if (infoBean.isFirstNews()) {// 头条
                    pushIconIv.setVisibility(View.VISIBLE);
                    pushIconIv.setImageResource(R.drawable.is_hot);
                }

                if (infoBean.isRead()) {
                    titleTv.setTextColor(activity.getResources().getColor(R.color.info_list_item_has_read_title));
                    descTv.setTextColor(activity.getResources().getColor(R.color.info_list_item_has_read_desc));
                }

                if (infoBean.getPic2Url() != null && !infoBean.getPic2Url().equals("")) {

                    ll.setVisibility(View.VISIBLE);

                    headIv.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.info_default_icon));

                    FinalBitmap finalBitMap = FinalBitmap.create(activity);
                    finalBitMap.configLoadingImage(R.drawable.info_default_icon);
                    finalBitMap.display(headIv, infoBean.getPic2Url());

                    // 使用该种方式加载图片拉动列表时存在问题
                    // ImageLoaderUtil.instance.setImageDrawable(infoBean.getPic2Path(),
                    // infoBean.getPic2Url(), headIv, true);
                    rowView.setTag(R.id.list_item_head_image, headIv);
                } else {
                    // ll.setVisibility(View.GONE);
                }

                int titleWordCount = AppUtil.getWordCount((int) ((Constants.SCREEN_WIDTH - headIv.getDrawable().getIntrinsicWidth() - 62)),
                    titleTv.getTextSize()) - 2;// -2是去除省略号占用的长度
                int descWordCount = AppUtil.getWordCount((int) ((Constants.SCREEN_WIDTH - headIv.getDrawable().getIntrinsicWidth() - 62) * 2),
                    descTv.getTextSize()) - 2;

                String titleStr = infoBean.getTitle();
                String descStr = infoBean.getContent();

                if (infoBean.getTitle().length() > titleWordCount) {
                    titleStr = infoBean.getTitle().substring(0, titleWordCount) + "...";
                }

                if (infoBean.getContent().length() > descWordCount) {
                    descStr = infoBean.getContent().substring(0, descWordCount) + "...";
                }
                titleTv.setText(titleStr);
                descTv.setText(descStr);
                rowView.setTag(infoBean);
            }
        }

        return rowView;
    }

    /**
     * 收索框点击
     */
    private OnClickListener searcheOnclick = new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(activity, "search", Toast.LENGTH_SHORT).show();
        }

    };

}
